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