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