xref: /linux/arch/x86/kernel/signal.c (revision b85d45947951d23cb22d90caecf4c1eb81342c96)
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/fpu/internal.h>
30 #include <asm/fpu/signal.h>
31 #include <asm/vdso.h>
32 #include <asm/mce.h>
33 #include <asm/sighandling.h>
34 #include <asm/vm86.h>
35 
36 #ifdef CONFIG_X86_64
37 #include <asm/proto.h>
38 #include <asm/ia32_unistd.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 #ifdef CONFIG_X86_32
97 		COPY_SEG_CPL3(cs);
98 		COPY_SEG_CPL3(ss);
99 #else /* !CONFIG_X86_32 */
100 		/* Kernel saves and restores only the CS segment register on signals,
101 		 * which is the bare minimum needed to allow mixed 32/64-bit code.
102 		 * App's signal handler can save/restore other segments if needed. */
103 		COPY_SEG_CPL3(cs);
104 #endif /* CONFIG_X86_32 */
105 
106 		get_user_ex(tmpflags, &sc->flags);
107 		regs->flags = (regs->flags & ~FIX_EFLAGS) | (tmpflags & FIX_EFLAGS);
108 		regs->orig_ax = -1;		/* disable syscall checks */
109 
110 		get_user_ex(buf, &sc->fpstate);
111 	} get_user_catch(err);
112 
113 	err |= fpu__restore_sig(buf, config_enabled(CONFIG_X86_32));
114 
115 	force_iret();
116 
117 	return err;
118 }
119 
120 int setup_sigcontext(struct sigcontext __user *sc, void __user *fpstate,
121 		     struct pt_regs *regs, unsigned long mask)
122 {
123 	int err = 0;
124 
125 	put_user_try {
126 
127 #ifdef CONFIG_X86_32
128 		put_user_ex(get_user_gs(regs), (unsigned int __user *)&sc->gs);
129 		put_user_ex(regs->fs, (unsigned int __user *)&sc->fs);
130 		put_user_ex(regs->es, (unsigned int __user *)&sc->es);
131 		put_user_ex(regs->ds, (unsigned int __user *)&sc->ds);
132 #endif /* CONFIG_X86_32 */
133 
134 		put_user_ex(regs->di, &sc->di);
135 		put_user_ex(regs->si, &sc->si);
136 		put_user_ex(regs->bp, &sc->bp);
137 		put_user_ex(regs->sp, &sc->sp);
138 		put_user_ex(regs->bx, &sc->bx);
139 		put_user_ex(regs->dx, &sc->dx);
140 		put_user_ex(regs->cx, &sc->cx);
141 		put_user_ex(regs->ax, &sc->ax);
142 #ifdef CONFIG_X86_64
143 		put_user_ex(regs->r8, &sc->r8);
144 		put_user_ex(regs->r9, &sc->r9);
145 		put_user_ex(regs->r10, &sc->r10);
146 		put_user_ex(regs->r11, &sc->r11);
147 		put_user_ex(regs->r12, &sc->r12);
148 		put_user_ex(regs->r13, &sc->r13);
149 		put_user_ex(regs->r14, &sc->r14);
150 		put_user_ex(regs->r15, &sc->r15);
151 #endif /* CONFIG_X86_64 */
152 
153 		put_user_ex(current->thread.trap_nr, &sc->trapno);
154 		put_user_ex(current->thread.error_code, &sc->err);
155 		put_user_ex(regs->ip, &sc->ip);
156 #ifdef CONFIG_X86_32
157 		put_user_ex(regs->cs, (unsigned int __user *)&sc->cs);
158 		put_user_ex(regs->flags, &sc->flags);
159 		put_user_ex(regs->sp, &sc->sp_at_signal);
160 		put_user_ex(regs->ss, (unsigned int __user *)&sc->ss);
161 #else /* !CONFIG_X86_32 */
162 		put_user_ex(regs->flags, &sc->flags);
163 		put_user_ex(regs->cs, &sc->cs);
164 		put_user_ex(0, &sc->gs);
165 		put_user_ex(0, &sc->fs);
166 #endif /* CONFIG_X86_32 */
167 
168 		put_user_ex(fpstate, &sc->fpstate);
169 
170 		/* non-iBCS2 extensions.. */
171 		put_user_ex(mask, &sc->oldmask);
172 		put_user_ex(current->thread.cr2, &sc->cr2);
173 	} put_user_catch(err);
174 
175 	return err;
176 }
177 
178 /*
179  * Set up a signal frame.
180  */
181 
182 /*
183  * Determine which stack to use..
184  */
185 static unsigned long align_sigframe(unsigned long sp)
186 {
187 #ifdef CONFIG_X86_32
188 	/*
189 	 * Align the stack pointer according to the i386 ABI,
190 	 * i.e. so that on function entry ((sp + 4) & 15) == 0.
191 	 */
192 	sp = ((sp + 4) & -16ul) - 4;
193 #else /* !CONFIG_X86_32 */
194 	sp = round_down(sp, 16) - 8;
195 #endif
196 	return sp;
197 }
198 
199 static inline void __user *
200 get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size,
201 	     void __user **fpstate)
202 {
203 	/* Default to using normal stack */
204 	unsigned long math_size = 0;
205 	unsigned long sp = regs->sp;
206 	unsigned long buf_fx = 0;
207 	int onsigstack = on_sig_stack(sp);
208 	struct fpu *fpu = &current->thread.fpu;
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 (fpu->fpstate_active) {
229 		sp = fpu__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 (fpu->fpstate_active &&
245 	    copy_fpstate_to_sigframe(*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 	sigset_t set;
543 
544 	frame = (struct sigframe __user *)(regs->sp - 8);
545 
546 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
547 		goto badframe;
548 	if (__get_user(set.sig[0], &frame->sc.oldmask) || (_NSIG_WORDS > 1
549 		&& __copy_from_user(&set.sig[1], &frame->extramask,
550 				    sizeof(frame->extramask))))
551 		goto badframe;
552 
553 	set_current_blocked(&set);
554 
555 	if (restore_sigcontext(regs, &frame->sc))
556 		goto badframe;
557 	return regs->ax;
558 
559 badframe:
560 	signal_fault(regs, frame, "sigreturn");
561 
562 	return 0;
563 }
564 #endif /* CONFIG_X86_32 */
565 
566 asmlinkage long sys_rt_sigreturn(void)
567 {
568 	struct pt_regs *regs = current_pt_regs();
569 	struct rt_sigframe __user *frame;
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))
581 		goto badframe;
582 
583 	if (restore_altstack(&frame->uc.uc_stack))
584 		goto badframe;
585 
586 	return regs->ax;
587 
588 badframe:
589 	signal_fault(regs, frame, "rt_sigreturn");
590 	return 0;
591 }
592 
593 static inline int is_ia32_compat_frame(void)
594 {
595 	return config_enabled(CONFIG_IA32_EMULATION) &&
596 	       test_thread_flag(TIF_IA32);
597 }
598 
599 static inline int is_ia32_frame(void)
600 {
601 	return config_enabled(CONFIG_X86_32) || is_ia32_compat_frame();
602 }
603 
604 static inline int is_x32_frame(void)
605 {
606 	return config_enabled(CONFIG_X86_X32_ABI) && test_thread_flag(TIF_X32);
607 }
608 
609 static int
610 setup_rt_frame(struct ksignal *ksig, struct pt_regs *regs)
611 {
612 	int usig = 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 stepping, failed;
633 	struct fpu *fpu = &current->thread.fpu;
634 
635 	if (v8086_mode(regs))
636 		save_v86_state((struct kernel_vm86_regs *) regs, VM86_SIGNAL);
637 
638 	/* Are we from a system call? */
639 	if (syscall_get_nr(current, regs) >= 0) {
640 		/* If so, check system call restarting.. */
641 		switch (syscall_get_error(current, regs)) {
642 		case -ERESTART_RESTARTBLOCK:
643 		case -ERESTARTNOHAND:
644 			regs->ax = -EINTR;
645 			break;
646 
647 		case -ERESTARTSYS:
648 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
649 				regs->ax = -EINTR;
650 				break;
651 			}
652 		/* fallthrough */
653 		case -ERESTARTNOINTR:
654 			regs->ax = regs->orig_ax;
655 			regs->ip -= 2;
656 			break;
657 		}
658 	}
659 
660 	/*
661 	 * If TF is set due to a debugger (TIF_FORCED_TF), clear TF now
662 	 * so that register information in the sigcontext is correct and
663 	 * then notify the tracer before entering the signal handler.
664 	 */
665 	stepping = test_thread_flag(TIF_SINGLESTEP);
666 	if (stepping)
667 		user_disable_single_step(current);
668 
669 	failed = (setup_rt_frame(ksig, regs) < 0);
670 	if (!failed) {
671 		/*
672 		 * Clear the direction flag as per the ABI for function entry.
673 		 *
674 		 * Clear RF when entering the signal handler, because
675 		 * it might disable possible debug exception from the
676 		 * signal handler.
677 		 *
678 		 * Clear TF for the case when it wasn't set by debugger to
679 		 * avoid the recursive send_sigtrap() in SIGTRAP handler.
680 		 */
681 		regs->flags &= ~(X86_EFLAGS_DF|X86_EFLAGS_RF|X86_EFLAGS_TF);
682 		/*
683 		 * Ensure the signal handler starts with the new fpu state.
684 		 */
685 		if (fpu->fpstate_active)
686 			fpu__clear(fpu);
687 	}
688 	signal_setup_done(failed, ksig, stepping);
689 }
690 
691 #ifdef CONFIG_X86_32
692 #define NR_restart_syscall	__NR_restart_syscall
693 #else /* !CONFIG_X86_32 */
694 #define NR_restart_syscall	\
695 	test_thread_flag(TIF_IA32) ? __NR_ia32_restart_syscall : __NR_restart_syscall
696 #endif /* CONFIG_X86_32 */
697 
698 /*
699  * Note that 'init' is a special process: it doesn't get signals it doesn't
700  * want to handle. Thus you cannot kill init even with a SIGKILL even by
701  * mistake.
702  */
703 void do_signal(struct pt_regs *regs)
704 {
705 	struct ksignal ksig;
706 
707 	if (get_signal(&ksig)) {
708 		/* Whee! Actually deliver the signal.  */
709 		handle_signal(&ksig, regs);
710 		return;
711 	}
712 
713 	/* Did we come from a system call? */
714 	if (syscall_get_nr(current, regs) >= 0) {
715 		/* Restart the system call - no handlers present */
716 		switch (syscall_get_error(current, regs)) {
717 		case -ERESTARTNOHAND:
718 		case -ERESTARTSYS:
719 		case -ERESTARTNOINTR:
720 			regs->ax = regs->orig_ax;
721 			regs->ip -= 2;
722 			break;
723 
724 		case -ERESTART_RESTARTBLOCK:
725 			regs->ax = NR_restart_syscall;
726 			regs->ip -= 2;
727 			break;
728 		}
729 	}
730 
731 	/*
732 	 * If there's no signal to deliver, we just put the saved sigmask
733 	 * back.
734 	 */
735 	restore_saved_sigmask();
736 }
737 
738 void signal_fault(struct pt_regs *regs, void __user *frame, char *where)
739 {
740 	struct task_struct *me = current;
741 
742 	if (show_unhandled_signals && printk_ratelimit()) {
743 		printk("%s"
744 		       "%s[%d] bad frame in %s frame:%p ip:%lx sp:%lx orax:%lx",
745 		       task_pid_nr(current) > 1 ? KERN_INFO : KERN_EMERG,
746 		       me->comm, me->pid, where, frame,
747 		       regs->ip, regs->sp, regs->orig_ax);
748 		print_vma_addr(" in ", regs->ip);
749 		pr_cont("\n");
750 	}
751 
752 	force_sig(SIGSEGV, me);
753 }
754 
755 #ifdef CONFIG_X86_X32_ABI
756 asmlinkage long sys32_x32_rt_sigreturn(void)
757 {
758 	struct pt_regs *regs = current_pt_regs();
759 	struct rt_sigframe_x32 __user *frame;
760 	sigset_t set;
761 
762 	frame = (struct rt_sigframe_x32 __user *)(regs->sp - 8);
763 
764 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
765 		goto badframe;
766 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
767 		goto badframe;
768 
769 	set_current_blocked(&set);
770 
771 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
772 		goto badframe;
773 
774 	if (compat_restore_altstack(&frame->uc.uc_stack))
775 		goto badframe;
776 
777 	return regs->ax;
778 
779 badframe:
780 	signal_fault(regs, frame, "x32 rt_sigreturn");
781 	return 0;
782 }
783 #endif
784