xref: /openbmc/linux/arch/x86/kernel/signal.c (revision 275876e2)
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 = current->mm->context.vdso +
302 			selected_vdso32->sym___kernel_sigreturn;
303 	else
304 		restorer = &frame->retcode;
305 	if (ksig->ka.sa.sa_flags & SA_RESTORER)
306 		restorer = ksig->ka.sa.sa_restorer;
307 
308 	/* Set up to return from userspace.  */
309 	err |= __put_user(restorer, &frame->pretcode);
310 
311 	/*
312 	 * This is popl %eax ; movl $__NR_sigreturn, %eax ; int $0x80
313 	 *
314 	 * WE DO NOT USE IT ANY MORE! It's only left here for historical
315 	 * reasons and because gdb uses it as a signature to notice
316 	 * signal handler stack frames.
317 	 */
318 	err |= __put_user(*((u64 *)&retcode), (u64 *)frame->retcode);
319 
320 	if (err)
321 		return -EFAULT;
322 
323 	/* Set up registers for signal handler */
324 	regs->sp = (unsigned long)frame;
325 	regs->ip = (unsigned long)ksig->ka.sa.sa_handler;
326 	regs->ax = (unsigned long)sig;
327 	regs->dx = 0;
328 	regs->cx = 0;
329 
330 	regs->ds = __USER_DS;
331 	regs->es = __USER_DS;
332 	regs->ss = __USER_DS;
333 	regs->cs = __USER_CS;
334 
335 	return 0;
336 }
337 
338 static int __setup_rt_frame(int sig, struct ksignal *ksig,
339 			    sigset_t *set, struct pt_regs *regs)
340 {
341 	struct rt_sigframe __user *frame;
342 	void __user *restorer;
343 	int err = 0;
344 	void __user *fpstate = NULL;
345 
346 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
347 
348 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
349 		return -EFAULT;
350 
351 	put_user_try {
352 		put_user_ex(sig, &frame->sig);
353 		put_user_ex(&frame->info, &frame->pinfo);
354 		put_user_ex(&frame->uc, &frame->puc);
355 
356 		/* Create the ucontext.  */
357 		if (cpu_has_xsave)
358 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
359 		else
360 			put_user_ex(0, &frame->uc.uc_flags);
361 		put_user_ex(0, &frame->uc.uc_link);
362 		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
363 
364 		/* Set up to return from userspace.  */
365 		restorer = current->mm->context.vdso +
366 			selected_vdso32->sym___kernel_rt_sigreturn;
367 		if (ksig->ka.sa.sa_flags & SA_RESTORER)
368 			restorer = ksig->ka.sa.sa_restorer;
369 		put_user_ex(restorer, &frame->pretcode);
370 
371 		/*
372 		 * This is movl $__NR_rt_sigreturn, %ax ; int $0x80
373 		 *
374 		 * WE DO NOT USE IT ANY MORE! It's only left here for historical
375 		 * reasons and because gdb uses it as a signature to notice
376 		 * signal handler stack frames.
377 		 */
378 		put_user_ex(*((u64 *)&rt_retcode), (u64 *)frame->retcode);
379 	} put_user_catch(err);
380 
381 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
382 	err |= 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 	regs->ax = (unsigned long)sig;
393 	regs->dx = (unsigned long)&frame->info;
394 	regs->cx = (unsigned long)&frame->uc;
395 
396 	regs->ds = __USER_DS;
397 	regs->es = __USER_DS;
398 	regs->ss = __USER_DS;
399 	regs->cs = __USER_CS;
400 
401 	return 0;
402 }
403 #else /* !CONFIG_X86_32 */
404 static int __setup_rt_frame(int sig, struct ksignal *ksig,
405 			    sigset_t *set, struct pt_regs *regs)
406 {
407 	struct rt_sigframe __user *frame;
408 	void __user *fp = NULL;
409 	int err = 0;
410 
411 	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe), &fp);
412 
413 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
414 		return -EFAULT;
415 
416 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
417 		if (copy_siginfo_to_user(&frame->info, &ksig->info))
418 			return -EFAULT;
419 	}
420 
421 	put_user_try {
422 		/* Create the ucontext.  */
423 		if (cpu_has_xsave)
424 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
425 		else
426 			put_user_ex(0, &frame->uc.uc_flags);
427 		put_user_ex(0, &frame->uc.uc_link);
428 		save_altstack_ex(&frame->uc.uc_stack, regs->sp);
429 
430 		/* Set up to return from userspace.  If provided, use a stub
431 		   already in userspace.  */
432 		/* x86-64 should always use SA_RESTORER. */
433 		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
434 			put_user_ex(ksig->ka.sa.sa_restorer, &frame->pretcode);
435 		} else {
436 			/* could use a vstub here */
437 			err |= -EFAULT;
438 		}
439 	} put_user_catch(err);
440 
441 	err |= setup_sigcontext(&frame->uc.uc_mcontext, fp, regs, set->sig[0]);
442 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
443 
444 	if (err)
445 		return -EFAULT;
446 
447 	/* Set up registers for signal handler */
448 	regs->di = sig;
449 	/* In case the signal handler was declared without prototypes */
450 	regs->ax = 0;
451 
452 	/* This also works for non SA_SIGINFO handlers because they expect the
453 	   next argument after the signal number on the stack. */
454 	regs->si = (unsigned long)&frame->info;
455 	regs->dx = (unsigned long)&frame->uc;
456 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
457 
458 	regs->sp = (unsigned long)frame;
459 
460 	/* Set up the CS register to run signal handlers in 64-bit mode,
461 	   even if the handler happens to be interrupting 32-bit code. */
462 	regs->cs = __USER_CS;
463 
464 	return 0;
465 }
466 #endif /* CONFIG_X86_32 */
467 
468 static int x32_setup_rt_frame(struct ksignal *ksig,
469 			      compat_sigset_t *set,
470 			      struct pt_regs *regs)
471 {
472 #ifdef CONFIG_X86_X32_ABI
473 	struct rt_sigframe_x32 __user *frame;
474 	void __user *restorer;
475 	int err = 0;
476 	void __user *fpstate = NULL;
477 
478 	frame = get_sigframe(&ksig->ka, regs, sizeof(*frame), &fpstate);
479 
480 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
481 		return -EFAULT;
482 
483 	if (ksig->ka.sa.sa_flags & SA_SIGINFO) {
484 		if (copy_siginfo_to_user32(&frame->info, &ksig->info))
485 			return -EFAULT;
486 	}
487 
488 	put_user_try {
489 		/* Create the ucontext.  */
490 		if (cpu_has_xsave)
491 			put_user_ex(UC_FP_XSTATE, &frame->uc.uc_flags);
492 		else
493 			put_user_ex(0, &frame->uc.uc_flags);
494 		put_user_ex(0, &frame->uc.uc_link);
495 		compat_save_altstack_ex(&frame->uc.uc_stack, regs->sp);
496 		put_user_ex(0, &frame->uc.uc__pad0);
497 
498 		if (ksig->ka.sa.sa_flags & SA_RESTORER) {
499 			restorer = ksig->ka.sa.sa_restorer;
500 		} else {
501 			/* could use a vstub here */
502 			restorer = NULL;
503 			err |= -EFAULT;
504 		}
505 		put_user_ex(restorer, &frame->pretcode);
506 	} put_user_catch(err);
507 
508 	err |= setup_sigcontext(&frame->uc.uc_mcontext, fpstate,
509 				regs, set->sig[0]);
510 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
511 
512 	if (err)
513 		return -EFAULT;
514 
515 	/* Set up registers for signal handler */
516 	regs->sp = (unsigned long) frame;
517 	regs->ip = (unsigned long) ksig->ka.sa.sa_handler;
518 
519 	/* We use the x32 calling convention here... */
520 	regs->di = ksig->sig;
521 	regs->si = (unsigned long) &frame->info;
522 	regs->dx = (unsigned long) &frame->uc;
523 
524 	loadsegment(ds, __USER_DS);
525 	loadsegment(es, __USER_DS);
526 
527 	regs->cs = __USER_CS;
528 	regs->ss = __USER_DS;
529 #endif	/* CONFIG_X86_X32_ABI */
530 
531 	return 0;
532 }
533 
534 /*
535  * Do a signal return; undo the signal stack.
536  */
537 #ifdef CONFIG_X86_32
538 asmlinkage unsigned long sys_sigreturn(void)
539 {
540 	struct pt_regs *regs = current_pt_regs();
541 	struct sigframe __user *frame;
542 	unsigned long ax;
543 	sigset_t set;
544 
545 	frame = (struct sigframe __user *)(regs->sp - 8);
546 
547 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
548 		goto badframe;
549 	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
550 		&& __copy_from_user(&set.sig[1], &frame->extramask,
551 				    sizeof(frame->extramask))))
552 		goto badframe;
553 
554 	set_current_blocked(&set);
555 
556 	if (restore_sigcontext(regs, &frame->sc, &ax))
557 		goto badframe;
558 	return ax;
559 
560 badframe:
561 	signal_fault(regs, frame, "sigreturn");
562 
563 	return 0;
564 }
565 #endif /* CONFIG_X86_32 */
566 
567 asmlinkage long sys_rt_sigreturn(void)
568 {
569 	struct pt_regs *regs = current_pt_regs();
570 	struct rt_sigframe __user *frame;
571 	unsigned long ax;
572 	sigset_t set;
573 
574 	frame = (struct rt_sigframe __user *)(regs->sp - sizeof(long));
575 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
576 		goto badframe;
577 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
578 		goto badframe;
579 
580 	set_current_blocked(&set);
581 
582 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
583 		goto badframe;
584 
585 	if (restore_altstack(&frame->uc.uc_stack))
586 		goto badframe;
587 
588 	return ax;
589 
590 badframe:
591 	signal_fault(regs, frame, "rt_sigreturn");
592 	return 0;
593 }
594 
595 /*
596  * OK, we're invoking a handler:
597  */
598 static int signr_convert(int sig)
599 {
600 #ifdef CONFIG_X86_32
601 	struct thread_info *info = current_thread_info();
602 
603 	if (info->exec_domain && info->exec_domain->signal_invmap && sig < 32)
604 		return info->exec_domain->signal_invmap[sig];
605 #endif /* CONFIG_X86_32 */
606 	return sig;
607 }
608 
609 static int
610 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
611 {
612 	int usig = signr_convert(ksig->sig);
613 	sigset_t *set = sigmask_to_save();
614 	compat_sigset_t *cset = (compat_sigset_t *) set;
615 
616 	/* Set up the stack frame */
617 	if (is_ia32_frame()) {
618 		if (ksig->ka.sa.sa_flags & SA_SIGINFO)
619 			return ia32_setup_rt_frame(usig, ksig, cset, regs);
620 		else
621 			return ia32_setup_frame(usig, ksig, cset, regs);
622 	} else if (is_x32_frame()) {
623 		return x32_setup_rt_frame(ksig, cset, regs);
624 	} else {
625 		return __setup_rt_frame(ksig->sig, ksig, set, regs);
626 	}
627 }
628 
629 static void
630 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
631 {
632 	bool failed;
633 	/* Are we from a system call? */
634 	if (syscall_get_nr(current, regs) >= 0) {
635 		/* If so, check system call restarting.. */
636 		switch (syscall_get_error(current, regs)) {
637 		case -ERESTART_RESTARTBLOCK:
638 		case -ERESTARTNOHAND:
639 			regs->ax = -EINTR;
640 			break;
641 
642 		case -ERESTARTSYS:
643 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
644 				regs->ax = -EINTR;
645 				break;
646 			}
647 		/* fallthrough */
648 		case -ERESTARTNOINTR:
649 			regs->ax = regs->orig_ax;
650 			regs->ip -= 2;
651 			break;
652 		}
653 	}
654 
655 	/*
656 	 * If TF is set due to a debugger (TIF_FORCED_TF), clear the TF
657 	 * flag so that register information in the sigcontext is correct.
658 	 */
659 	if (unlikely(regs->flags & X86_EFLAGS_TF) &&
660 	    likely(test_and_clear_thread_flag(TIF_FORCED_TF)))
661 		regs->flags &= ~X86_EFLAGS_TF;
662 
663 	failed = (setup_rt_frame(ksig, regs) < 0);
664 	if (!failed) {
665 		/*
666 		 * Clear the direction flag as per the ABI for function entry.
667 		 *
668 		 * Clear RF when entering the signal handler, because
669 		 * it might disable possible debug exception from the
670 		 * signal handler.
671 		 *
672 		 * Clear TF when entering the signal handler, but
673 		 * notify any tracer that was single-stepping it.
674 		 * The tracer may want to single-step inside the
675 		 * handler too.
676 		 */
677 		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
678 	}
679 	signal_setup_done(failed, ksig, test_thread_flag(TIF_SINGLESTEP));
680 }
681 
682 #ifdef CONFIG_X86_32
683 #define NR_restart_syscall	__NR_restart_syscall
684 #else /* !CONFIG_X86_32 */
685 #define NR_restart_syscall	\
686 	test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
687 #endif /* CONFIG_X86_32 */
688 
689 /*
690  * Note that 'init' is a special process: it doesn't get signals it doesn't
691  * want to handle. Thus you cannot kill init even with a SIGKILL even by
692  * mistake.
693  */
694 static void do_signal(struct pt_regs *regs)
695 {
696 	struct ksignal ksig;
697 
698 	if (get_signal(&ksig)) {
699 		/* Whee! Actually deliver the signal.  */
700 		handle_signal(&ksig, regs);
701 		return;
702 	}
703 
704 	/* Did we come from a system call? */
705 	if (syscall_get_nr(current, regs) >= 0) {
706 		/* Restart the system call - no handlers present */
707 		switch (syscall_get_error(current, regs)) {
708 		case -ERESTARTNOHAND:
709 		case -ERESTARTSYS:
710 		case -ERESTARTNOINTR:
711 			regs->ax = regs->orig_ax;
712 			regs->ip -= 2;
713 			break;
714 
715 		case -ERESTART_RESTARTBLOCK:
716 			regs->ax = NR_restart_syscall;
717 			regs->ip -= 2;
718 			break;
719 		}
720 	}
721 
722 	/*
723 	 * If there's no signal to deliver, we just put the saved sigmask
724 	 * back.
725 	 */
726 	restore_saved_sigmask();
727 }
728 
729 /*
730  * notification of userspace execution resumption
731  * - triggered by the TIF_WORK_MASK flags
732  */
733 __visible void
734 do_notify_resume(struct pt_regs *regs, void *unused, __u32 thread_info_flags)
735 {
736 	user_exit();
737 
738 #ifdef CONFIG_X86_MCE
739 	/* notify userspace of pending MCEs */
740 	if (thread_info_flags & _TIF_MCE_NOTIFY)
741 		mce_notify_process();
742 #endif /* CONFIG_X86_64 && CONFIG_X86_MCE */
743 
744 	if (thread_info_flags & _TIF_UPROBE)
745 		uprobe_notify_resume(regs);
746 
747 	/* deal with pending signal delivery */
748 	if (thread_info_flags & _TIF_SIGPENDING)
749 		do_signal(regs);
750 
751 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
752 		clear_thread_flag(TIF_NOTIFY_RESUME);
753 		tracehook_notify_resume(regs);
754 	}
755 	if (thread_info_flags & _TIF_USER_RETURN_NOTIFY)
756 		fire_user_return_notifiers();
757 
758 	user_enter();
759 }
760 
761 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
762 {
763 	struct task_struct *me = current;
764 
765 	if (show_unhandled_signals && printk_ratelimit()) {
766 		printk("%s"
767 		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
768 		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
769 		       me->comm, me->pid, where, frame,
770 		       regs->ip, regs->sp, regs->orig_ax);
771 		print_vma_addr(" in ", regs->ip);
772 		pr_cont("\n");
773 	}
774 
775 	force_sig(SIGSEGV, me);
776 }
777 
778 #ifdef CONFIG_X86_X32_ABI
779 asmlinkage long sys32_x32_rt_sigreturn(void)
780 {
781 	struct pt_regs *regs = current_pt_regs();
782 	struct rt_sigframe_x32 __user *frame;
783 	sigset_t set;
784 	unsigned long ax;
785 
786 	frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
787 
788 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
789 		goto badframe;
790 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
791 		goto badframe;
792 
793 	set_current_blocked(&set);
794 
795 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext, &ax))
796 		goto badframe;
797 
798 	if (compat_restore_altstack(&frame->uc.uc_stack))
799 		goto badframe;
800 
801 	return ax;
802 
803 badframe:
804 	signal_fault(regs, frame, "x32 rt_sigreturn");
805 	return 0;
806 }
807 #endif
808